package com.oa.framework.acl.url.service;

import com.oa.framework.acl.ResourceType;
import com.oa.framework.acl.url.domain.SystemUrlResource;
import com.oa.framework.acl.url.dao.SystemUrlResourceDao;
import com.oa.framework.cache.UrlResourceCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @author cuichao
 */
@Service
public class SystemUrlResourceServiceImpl implements SystemUrlResourceService {

    private final LinkedHashSet<SystemUrlResource> EMPTY = new LinkedHashSet<>();

    @Autowired
    private SystemUrlResourceDao resourceDao;

    @Autowired
    private UrlResourceCache cache;


    @Override
    public LinkedHashSet<SystemUrlResource> loadSecurityResources() {
        LinkedHashSet<SystemUrlResource> resource = cache.get();

        if (resource != null) {
            return resource;
        }

        List<SystemUrlResource> resourceList = resourceDao.selectList(null);
        LinkedHashSet<SystemUrlResource> resources = new LinkedHashSet<>();
        if (CollectionUtils.isEmpty(resourceList)) {
            return EMPTY;
        }
        resources.addAll(resourceList);

        cache.put(resources);
        return resources;
    }

    @Override
    public LinkedHashSet<SystemUrlResource> getFoldResource() {
        LinkedHashSet<SystemUrlResource> fold = cache.getFold();

        if (fold != null) {
            return fold;
        }

        LinkedHashSet<SystemUrlResource> resources = loadSecurityResources();
        if (CollectionUtils.isEmpty(resources)) {
            return EMPTY;
        }
        fold = doGetFoldResource(resources);
        if (CollectionUtils.isEmpty(fold)) {
            return fold;
        }
        cache.putFold(resources);
        return fold;
    }


    /**
     * 折叠系统资源
     *
     * @param resources
     * @return
     */
    private LinkedHashSet<SystemUrlResource> doGetFoldResource(LinkedHashSet<SystemUrlResource> resources) {
        LinkedHashSet<SystemUrlResource> fold = new LinkedHashSet<>();
        /**
         * fold
         */
        for (SystemUrlResource resource : resources) {
            boolean isRoot = resource.getParentId() == null || resource.getParentId() <= 0;
            if (isRoot && ResourceType.menu.equals(resource.getResourceType())) {
                fold.add(resource);
                recursionResource(resources, resource);
            }
        }
        /**
         *  order not fold
         */
        for (SystemUrlResource resource : resources) {
            if (!ResourceType.menu.equals(resource.getResourceType())) {
                fold.add(resource);
            }
        }
        return fold;
    }

    /**
     * 递归折叠子节点
     *
     * @param resources
     * @param parent
     * @return
     */
    private void recursionResource(LinkedHashSet<SystemUrlResource> resources, SystemUrlResource parent) {
        for (SystemUrlResource resource : resources) {
            if (parent.getResourceId().equals(resource.getParentId()) && ResourceType.menu.equals(resource.getResourceType())) {
                if (parent.getChildren() == null) {
                    parent.setChildren(new ArrayList<>());
                }
                resource.setParent(parent);
                parent.getChildren().add(resource);
                recursionResource(resources, resource);
                if (CollectionUtils.isEmpty(resource.getChildren())) {
                    return;
                }
            }
        }
    }


}
